/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.nokia.mid.ui.orientation.Orientation;
import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.*;
import org.netbeans.microedition.lcdui.SplashScreen;

/**
 * @author Windsea
 */
public class App extends MIDlet implements CommandListener {

    public static App app;
    public static Display dis;
    public UIMain uiMain;
    public ColorSelectorUI cls;
    private StringItem about, guide, chooseColor;
    Command cmdBack, cmdChooseColor;

    public App() {
        Orientation.setAppOrientation(2);
        R.initR();
        app = this;
        dis = Display.getDisplay(this);
    }

    public static void quitApp() {
        try {
            app.destroyApp(true);
            app.notifyDestroyed();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void exit() {
        destroyApp(false);
        notifyDestroyed();
    }

    public void startApp() {
        SplashScreen spl = new SplashScreen(dis);
        try {
            spl.setImage(Image.createImage("/logo.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        spl.setTimeout(3000);
        spl.setFullScreenMode(true);
        uiMain = new UIMain(app);
        spl.setNextDisplayable(uiMain);
        dis.setCurrent(spl);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        destroyApp(true);
        notifyDestroyed();
    }

    public void showMenuForm() {
        Form form = new Form("Setting");
        chooseColor = new StringItem("", "Choose BG color", StringItem.BUTTON);
        about = new StringItem("About", "Piano In Hand\nVendor: Windsea\nVersion: 1.0");
        guide = new StringItem("Introduce", "Piano In Hand have 7 key: DO, RE, MI, FA, SON, LA, SI. Play it with fun.");
        cmdBack = new Command("Back", Command.BACK, 0);
        cmdChooseColor = new Command("Choose BG Color", Command.ITEM, 0);
        form.append(chooseColor);
        form.append(about);
        form.append(guide);
        form.addCommand(cmdBack);
        form.addCommand(cmdChooseColor);
        form.setCommandListener(this);

        ItemCommandListener icl = new ItemCommandListener() {
            public void commandAction(Command c, Item item) {

                cls = new ColorSelectorUI(app);
                dis.setCurrent(cls);
            }
        };
        chooseColor.setDefaultCommand(cmdChooseColor);
        chooseColor.setItemCommandListener(icl);
        dis.setCurrent(form);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdBack) {
            Orientation.setAppOrientation(Orientation.ORIENTATION_LANDSCAPE);
            dis.setCurrent(uiMain);
        } else if (c == cmdChooseColor) {
            cls = new ColorSelectorUI(this);
            dis.setCurrent(cls);
        }
    }

    public UIMain getUIMain() {
        return uiMain;
    }
}
